home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dl / dl_checkrange.c next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  4.6 KB  |  163 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /*
  25. ** dl_checkrange - Check that a given address range is free.
  26. */
  27. #include "dl.h"
  28. #include <string.h>
  29.  
  30. #ifdef DEBUG
  31. #define D(x) (x)
  32. #else
  33. #define D(x)
  34. #endif /* DEBUG */
  35.  
  36. /* Macro's to handle bitmaps */
  37. #define IND(x) ((x)>>5)
  38. #define BIT(x) ((x)&0x1f)
  39.  
  40. #define ISSET(map,x) (map[IND(x)] & (1<<BIT(x)))
  41. #define DOSET(map,x) (map[IND(x)] |= (1<<BIT(x)))
  42.  
  43. /* Macros to convert addresses to segment indices */
  44. #define SEGSIZE 0x00400000        /* 4Mb segsize */
  45. #define BTOS(x) ((x)/SEGSIZE)
  46.  
  47. #define MAXADDR 0x7fffffff        /* Max user address */
  48. #define NSEG    (BTOS(MAXADDR)+1)    /* Number of segments */
  49.  
  50. /* Other stuff */
  51. #define MINTEXTADDR    0x00400000    /* Min text address */
  52. #define MAXTEXTADDR    0x0bffffff
  53. #define MINLIBADDR    0x0c000000    /* Where shared libs are loaded */
  54. #define MAXLIBADDR    0x0fffffff
  55. #define MINDATAADDR    0x10000000    /* Where normal data is loaded */
  56. #define MAXDATAADDR    0x7fffffff    
  57. #define HEADROOM    0x01000000    /* Headroom for data space */
  58.  
  59. #define NINRANGE(min, max) ((max-min)/SEGSIZE)
  60.  
  61. /* The map */
  62. long map[IND(NSEG)];
  63. int map_inited;
  64.  
  65. static void initmap(void) {
  66.     extern etext, end;
  67.     long i;
  68.  
  69.     if ( map_inited ) return;
  70.     D(printf("Initializing map, size %d\n", IND(NSEG)));
  71.     for ( i=0; i<=(long)&etext; i+=SEGSIZE)
  72.       DOSET(map, BTOS(i));
  73.     for ( i=MINLIBADDR; i<=MAXLIBADDR; i+=SEGSIZE)
  74.       DOSET(map, BTOS(i));
  75.     for ( i=MINDATAADDR; i<(long)&end+HEADROOM; i+=SEGSIZE)
  76.       DOSET(map, BTOS(i));
  77.     map_inited = 1;
  78. }
  79.  
  80. static int
  81. findfree(long int min, long int max, long int pref, long int len)
  82. {
  83.     long try = pref;
  84.     long tlen;
  85.  
  86.     if ( try % SEGSIZE ) {
  87.     dl_error("Internal error: non-aligned address in checkrange", 0);
  88.     return 0;
  89.     }
  90.     while(1) {
  91.     /* Find a free slot where we can start to search */
  92.     while( ISSET(map, BTOS(try))) {
  93.         try+=SEGSIZE;
  94.         if ( try >= max ) try = min;
  95.         if ( try == pref ) return 0;
  96.     }
  97.     /* If there's a chance of fitting it here see if the range is free */
  98.     if ( try + len <= max ) {
  99.         tlen = 0;
  100.         while ( !ISSET(map, BTOS(try+tlen)) ) {
  101.         if ( tlen >= len ) return try;
  102.         tlen += SEGSIZE;
  103.         }
  104.     }
  105.     }
  106. }
  107.  
  108. int dl_checkrange(long int beg, long int len)
  109. {
  110.     long a;
  111.     long end = beg + len;
  112.  
  113.     D(printf("Checkrange(%x to %x)\n", beg, end));
  114.     initmap();
  115.     for( a=beg; a<=end; a += SEGSIZE )
  116.       if ( ISSET(map, BTOS(a)) )
  117.         return 0;
  118.     return 1;
  119. }
  120.  
  121. int dl_setrange(long int beg, long int end)
  122. {
  123.     long a;
  124.  
  125.     initmap();
  126.     for ( a=beg; a<=end; a += SEGSIZE ) {
  127.     if ( ISSET(map, BTOS(a)) ) {
  128.         dl_error("Internal error: allocating used address", 0);
  129.         return 0;
  130.     }
  131.     DOSET(map, BTOS(a));
  132.     }
  133.     return 1;
  134. }
  135.  
  136. int
  137. dl_hashaddrs(char *name, long int tlen, long int dlen, long int *taddrp, long int *daddrp)
  138. {
  139.     unsigned long hashval;
  140.     long taddr, daddr;
  141.     int i;
  142.     char *p;
  143.  
  144.     initmap();
  145.     hashval = 0;
  146.     p = strrchr(name, '/');
  147.     if ( p == 0 ) p = name;
  148.     D(printf("dl_hashaddrs(%s(%s), %x, %x)\n", name, p, tlen, dlen));
  149.     for(i=0; name[i]; i++) {
  150.       hashval += name[i]*i;
  151.     }
  152.     taddr = (hashval%NINRANGE(MINTEXTADDR, MAXTEXTADDR))*SEGSIZE + MINTEXTADDR;
  153.     daddr = (hashval%NINRANGE(MINDATAADDR, MAXDATAADDR))*SEGSIZE + MINDATAADDR;
  154.     D(printf("dl_hashaddrs: 0x%x, 0x%x\n", taddr, daddr));
  155.     taddr = findfree(MINTEXTADDR, MAXTEXTADDR, taddr, tlen);
  156.     daddr = findfree(MINDATAADDR, MAXDATAADDR, daddr, dlen);
  157.     D(printf("dl_hashaddrs: using 0x%x 0x%x\n", taddr, daddr));
  158.     if ( taddr == 0 || daddr == 0 ) return 0;
  159.     *taddrp = taddr;
  160.     *daddrp = daddr;
  161.     return 1;
  162. }
  163.